<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class userController extends Controller
{
    //用户添加
    // public function index()
    // {	
    // 	//原声语句
    // 	$list = DB::table('user')->paginate(3);

    // 	return view('admin.user.list',['list'=>$list]);

    //     // // 保存搜索搜索
    //     // $where = [];
    // }

      //用户添加
    public function index(Request $request)
    {   
       // 保存搜索的条件
        $where = [];
        $db = DB::table('user');
        if($request->input('username') != ''){
            $where['username'] = $request->input('username');
            $list = $db->where('username','like',"%{$request->input('username')}%")->paginate(4);  
        }else{
            $list = $db->paginate(4);
        }    
        return view('admin.user.list',['list'=>$list,'where'=>$where]);
    }

    //显示添加
    public function create(Request $request)
    {
       return view('admin.user.add');
    }

    //执行添加
	public function store(Request $request)
    {

    	$data = $request->except('_token');
    	$row = DB::table('user')->insert($data);
    	if($row>0){
    		return redirect('/admin/user')->with('msg','添加成功');
    	}
    }

    //显示修改
     public function edit($id)
    {
    	$username = DB::table('user')->where('id',$id)->first();
    	return view('admin.user.edit',['username'=>$username]);
    }
    //执行修改
    public function update($id,Request $request)
    {
    	// 获取除了指定字段，剩下的字段
    	$data = $request->except('_token','_method');
    	 //dd($data);
    	$row = DB::table('user')->where('id',$id)->update($data);
    	//dd($row);
    	if($row>0){
    		return redirect('/admin/user')->with('msg','修改成功');
    	}
    }

    // 删除
    public function destroy($id)
    {
    	$row = DB::table('user')->where('id',$id)->delete();
    	if($row>0){
    		return redirect('/admin/user/')->with('msg','删除成功');
    	}
    }
}
